package com.molyfun.parents.activity.account;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.view.SMSCountDownTimer;
import com.molyfun.parents.view.SinglineClearEditText;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.smssdk.SMSSDK;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ForgetPwdActivity extends BaseActivity implements View.OnClickListener {


    @InjectView(R.id.base_title)
    TextView mTitle;

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.phone_num)
    SinglineClearEditText mPhoneNum;
    @InjectView(R.id.code)
    SinglineClearEditText mCode;
    @InjectView(R.id.get_code)
    TextView mGetCode;
    @InjectView(R.id.activity_login_pwd)
    SinglineClearEditText mLoginPwd;
    @InjectView(R.id.commit)
    Button mCommit;

    SMSCountDownTimer smsCountDownTimer;

    private boolean isPhoneInput = false;
    private boolean isCodeInput = false;
    private boolean isPwdInput = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");
        mTitle.setText("您正在重置密码");

        mBack.setOnClickListener(this);
        mCommit.setOnClickListener(this);
        mGetCode.setOnClickListener(this);
        mPhoneNum.setHint("请输入您的预留手机号码");
        mCode.setHint("请输入您的验证码");
        mLoginPwd.setHint("请设置至少六位密码");

        mLoginPwd.setIsPwd();

        smsCountDownTimer = new SMSCountDownTimer(mGetCode, 60000, 1000);

        mPhoneNum.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() > 0){
                    isPhoneInput = true;
                }else{
                    isPhoneInput = false;
                }
                changeCommitBtnState();
            }
        });
        mCode.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() > 0){
                    isCodeInput = true;
                }else{
                    isCodeInput = false;
                }
                changeCommitBtnState();
            }
        });
        mLoginPwd.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() > 0){
                    isPwdInput = true;
                }else{
                    isPwdInput = false;
                }
                changeCommitBtnState();
            }
        });

    }

    private void changeCommitBtnState(){
        if(isPhoneInput && isCodeInput && isPwdInput){
            mCommit.setEnabled(true);
        }else{
            mCommit.setEnabled(false);
        }
    }

    @Override
    protected void onDestroy() {
        SMSSDK.unregisterAllEventHandler();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.commit:
                resetPwd(mPhoneNum.getString().trim(), mLoginPwd.getString().trim(), mCode.getString().trim());
                break;
            case R.id.get_code:
                if(TextUtils.isEmpty(mPhoneNum.getString().trim())){
                    toast("请输入手机号");
                    return;
                }
                Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().getSmsCode(mPhoneNum.getString().trim(), "recoverpass");
                call.enqueue(new Callback<BaseResponse>() {
                    @Override
                    public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                        if(response.body() != null){
                            if(response.body().getCode() == 200){
                                toast("验证码发送成功");
                                smsCountDownTimer.start();
                            }else {
                                toast(response.body().getMsg());
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseResponse> call, Throwable t) {
                        toast("请求失败");
                    }
                });

                break;
            default:
                break;
        }
    }

    private void resetPwd(String phone, String pwd, String captchacode){
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().recoverPassword(phone, pwd, captchacode);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                if(response.body() != null){
                    if(response.body().getCode() == 200){
                        toast("修改成功");
                        LoginActivity.startActivity(ForgetPwdActivity.this);
                        finish();
                    }else{
                        toast(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                toast("失败");
            }
        });
    }

}
